<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>制作和上传C/C++包到xmake的官方仓库</title>
    <meta name="description" content="xmake集成了内置的远程包依赖管理，用户只需要简单地在项目中添加自己所需要的包和版本，即可自动下载和集成对应的包到项目中，并且实现编译和链接。例如：add_requires("libuv master", "ffmpeg", "zlib 1.20.*")add_requires("tbox &gt;1.6.1"...">

    
    <meta name="keywords" content="xmake,lua,C/C++,包仓库,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2019/08/09/xmake-upload-package/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>制作和上传C/C++包到xmake的官方仓库</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2019-08-09
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#lua" title="Tag: lua" rel="tag">lua</a-->
        <a href="/cn/tag/#lua" title="Tag: lua" rel="tag">lua</a>&nbsp;
    
        <!--a href="/tag/#C%2FC%2B%2B" title="Tag: C/C++" rel="tag">C/C++</a-->
        <a href="/cn/tag/#C/C++" title="Tag: C/C++" rel="tag">C/C++</a>&nbsp;
    
        <!--a href="/tag/#%E5%8C%85%E4%BB%93%E5%BA%93" title="Tag: 包仓库" rel="tag">包仓库</a-->
        <a href="/cn/tag/#包仓库" title="Tag: 包仓库" rel="tag">包仓库</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>xmake集成了内置的远程包依赖管理，用户只需要简单地在项目中添加自己所需要的包和版本，即可自动下载和集成对应的包到项目中，并且实现编译和链接。</p>

<p>例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"libuv master"</span><span class="p">,</span> <span class="s2">"ffmpeg"</span><span class="p">,</span> <span class="s2">"zlib 1.20.*"</span><span class="p">)</span>
<span class="n">add_requires</span><span class="p">(</span><span class="s2">"tbox &gt;1.6.1"</span><span class="p">,</span> <span class="p">{</span><span class="n">optional</span> <span class="o">=</span> <span class="kc">true</span><span class="p">,</span> <span class="n">debug</span> <span class="o">=</span> <span class="kc">true</span><span class="p">})</span>
<span class="n">add_requires</span><span class="p">(</span><span class="s2">"boost"</span><span class="p">,</span> <span class="p">{</span><span class="n">alias</span> <span class="o">=</span> <span class="s2">"boost_context"</span><span class="p">,</span> <span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">context</span> <span class="o">=</span> <span class="kc">true</span><span class="p">}})</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_packages</span><span class="p">(</span><span class="s2">"libuv"</span><span class="p">,</span> <span class="s2">"ffmpeg"</span><span class="p">,</span> <span class="s2">"tbox"</span><span class="p">,</span> <span class="s2">"boost_context"</span><span class="p">,</span> <span class="s2">"zlib"</span><span class="p">)</span>
</code></pre>
</div>

<p>xmake的包仓库设计之初，就考虑到了语义版本支持，以及依赖包的跨平台支持，只要包自身能支持的平台，都可以集成进来，比如zlib包，在xmake中使用，iphoneos, android以及mingw平台下都是完全可用的。</p>

<p>用户只需要简单的切下构建平台：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code>xmake f -p iphoneos -a arm64
xmake
note: try installing these packages <span class="o">(</span>pass -y to skip confirm<span class="o">)</span>?
<span class="k">in </span>xmake-repo:
  -&gt; zlib 1.2.11 
please input: y <span class="o">(</span>y/n<span class="o">)</span>
  <span class="o">=</span>&gt; download https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz .. ok                                                                               
  <span class="o">=</span>&gt; install zlib 1.2.11 .. ok   
</code></pre>
</div>

<p>就可以对iphoneos平台下载集成<code class="highlighter-rouge">add_requires</code>中对应的包，xmake的最终目标，是打造一个跨平台的包仓库，用户不再需要满地找c/c++库，然后研究各种平台的移植，只需要简单的添加上包依赖，即可在各个平台都能方便使用。</p>

<p>当然了，目前xmake的官方仓库还在发展初期，里面的包还很少，支持的平台也不是很完善，因此，这里我简单介绍下用户如何去自己制作和上传自己需要的c/c++包，并如何提交到我们的仓库中（也可以自建私有仓库），
希望有兴趣的小伙伴可以帮忙贡献一份微薄之力，一起共同打造和建立c/c++依赖包生态。</p>

<ul>
  <li><a href="https://github.com/xmake-io/xmake">项目源码</a></li>
  <li><a href="https://xmake.io/#/zh/">官方文档</a></li>
</ul>

<h2 id="添加包到仓库">添加包到仓库</h2>

<h3 id="仓库包结构">仓库包结构</h3>

<p>在制作自己的包之前，我们需要先了解下一个包仓库的结构，不管是官方包仓库，还是自建私有包仓库，结构都是相同的：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>xmake-repo
  - packages
    - t/tbox/xmake.lua
    - z/zlib/xmake.lua
</code></pre>
</div>

<p>通过上面的结构，可以看到每个包都会有个xmake.lua用于描述它的安装规则，并且根据<code class="highlighter-rouge">z/zlib</code>两级子目录分类存储，方便快速检索。</p>

<h3 id="包描述说明">包描述说明</h3>

<p>关于包的描述规则，基本上都是在它的xmake.lua里面完成的，这跟项目工程里面的xmake.lua描述很类似，不同的是描述域仅支持<code class="highlighter-rouge">package()</code>，</p>

<p>不过，在项目xmake.lua里面，也是可以直接添加<code class="highlighter-rouge">package()</code>来内置包描述的，连包仓库都省了，有时候这样会更加方便。</p>

<p>首先，我们先拿zlib的描述规则，来直观感受下，这个规则可以在<a href="https://github.com/xmake-io/xmake-repo/blob/master/packages/z/zlib/xmake.lua">xmake-repo/z/zlib/xmake.lua</a>下找到。</p>

<div class="highlighter-rouge"><pre class="highlight"><code>package("zlib")

    set_homepage("http://www.zlib.net")
    set_description("A Massively Spiffy Yet Delicately Unobtrusive Compression Library")

    set_urls("http://zlib.net/zlib-$(version).tar.gz",
             "https://downloads.sourceforge.net/project/libpng/zlib/$(version)/zlib-$(version).tar.gz")

    add_versions("1.2.10", "8d7e9f698ce48787b6e1c67e6bff79e487303e66077e25cb9784ac8835978017")
    add_versions("1.2.11", "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1")

    on_install("windows", function (package)
        io.gsub("win32/Makefile.msc", "%-MD", "-" .. package:config("vs_runtime"))
        os.vrun("nmake -f win32\\Makefile.msc zlib.lib")
        os.cp("zlib.lib", package:installdir("lib"))
        os.cp("*.h", package:installdir("include"))
    end)

    on_install("linux", "macosx", function (package)
        import("package.tools.autoconf").install(package, {"--static"})
    end)
 
    on_install("iphoneos", "android@linux,macosx", "mingw@linux,macosx", function (package)
        import("package.tools.autoconf").configure(package, {host = "", "--static"})
        io.gsub("Makefile", "\nAR=.-\n",      "\nAR=" .. (package:build_getenv("ar") or "") .. "\n")
        io.gsub("Makefile", "\nARFLAGS=.-\n", "\nARFLAGS=cr\n")
        io.gsub("Makefile", "\nRANLIB=.-\n",  "\nRANLIB=\n")
        os.vrun("make install -j4")
    end)

    on_test(function (package)
        assert(package:has_cfuncs("inflate", {includes = "zlib.h"}))
    end)
</code></pre>
</div>

<p>这个包规则对windows, linux, macosx, iphoneos，mingw等平台都添加了安装规则，基本上已经做到了全平台覆盖，甚至一些交叉编译平台，算是一个比较典型的例子了。</p>

<p>当然，有些包依赖源码实现力度，并不能完全跨平台，那么只需对它支持的平台设置安装规则即可。</p>

<h4 id="set_homepage">set_homepage</h4>

<p>设置包所在项目的官方页面地址。</p>

<h4 id="set_description">set_description</h4>

<p>设置包的相关描述信息，一般通过<code class="highlighter-rouge">xmake require --info zlib</code>查看相关包信息时候，会看到。</p>

<h4 id="set_kind">set_kind</h4>

<p>设置包类型，对于依赖库，则不用设置，如果是可执行包，需要设置为binary。</p>

<div class="highlighter-rouge"><pre class="highlight"><code>package("cmake")

    set_kind("binary")
    set_homepage("https://cmake.org")
    set_description("A cross-platform family of tool designed to build, test and package software")
</code></pre>
</div>

<h4 id="set_urls">set_urls</h4>

<p>设置包的源码包或者git仓库地址，跟add_urls不同的是，此接口是覆盖性设置，而add_urls是追加设置，其他使用方式类似，这个根据不同需要来选择。</p>

<h4 id="add_urls">add_urls</h4>

<p>添加包的源码包或者git仓库地址，此接口一般跟add_version配对使用，用于设置每个源码包的版本和对应的sha256值。</p>

<p>!&gt; 可以通过添加多个urls作为镜像源，xmake会自动检测优先选用最快的url进行下载，如果下载失败则会尝试其他urls。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_urls</span><span class="p">(</span><span class="s2">"https://github.com/protobuf-c/protobuf-c/releases/download/v$(version)/protobuf-c-$(version).tar.gz"</span><span class="p">)</span>
<span class="n">add_versions</span><span class="p">(</span><span class="s2">"1.3.1"</span><span class="p">,</span> <span class="s2">"51472d3a191d6d7b425e32b612e477c06f73fe23e07f6a6a839b11808e9d2267"</span><span class="p">)</span>
</code></pre>
</div>

<p>urls里面的<code class="highlighter-rouge">$(version)</code>内置变量，会根据实际安装时候选择的版本适配进去，而版本号都是从<code class="highlighter-rouge">add_versions</code>中指定的版本列表中选择的。</p>

<p>如果对于urls里面带有比较复杂的版本串，没有跟add_versions有直接对应关系，则需要通过下面的方式定制化转换下：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_urls</span><span class="p">(</span><span class="s2">"https://sqlite.org/2018/sqlite-autoconf-$(version)000.tar.gz"</span><span class="p">,</span>
         <span class="p">{</span><span class="n">version</span> <span class="o">=</span> <span class="k">function</span> <span class="p">(</span><span class="n">version</span><span class="p">)</span> <span class="k">return</span> <span class="n">version</span><span class="p">:</span><span class="n">gsub</span><span class="p">(</span><span class="s2">"%."</span><span class="p">,</span> <span class="s2">""</span><span class="p">)</span> <span class="k">end</span><span class="p">})</span>

<span class="n">add_versions</span><span class="p">(</span><span class="s2">"3.24.0"</span><span class="p">,</span> <span class="s2">"d9d14e88c6fb6d68de9ca0d1f9797477d82fc3aed613558f87ffbdbbc5ceb74a"</span><span class="p">)</span>
<span class="n">add_versions</span><span class="p">(</span><span class="s2">"3.23.0"</span><span class="p">,</span> <span class="s2">"b7711a1800a071674c2bf76898ae8584fc6c9643cfe933cfc1bc54361e3a6e49"</span><span class="p">)</span>
</code></pre>
</div>

<p>当然，我们也只可以添加git源码地址：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_urls</span><span class="p">(</span><span class="s2">"https://gitlab.gnome.org/GNOME/libxml2.git"</span><span class="p">)</span>
</code></pre>
</div>

<p>如果设置的多个镜像地址对应的源码包sha256是不同的，我们可以通过alias的方式来分别设置</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_urls</span><span class="p">(</span><span class="s2">"https://ffmpeg.org/releases/ffmpeg-$(version).tar.bz2"</span><span class="p">,</span> <span class="p">{</span><span class="n">alias</span> <span class="o">=</span> <span class="s2">"home"</span><span class="p">})</span>
<span class="n">add_urls</span><span class="p">(</span><span class="s2">"https://github.com/FFmpeg/FFmpeg/archive/n$(version).zip"</span><span class="p">,</span> <span class="p">{</span><span class="n">alias</span> <span class="o">=</span> <span class="s2">"github"</span><span class="p">})</span>
<span class="n">add_versions</span><span class="p">(</span><span class="s2">"home:4.0.2"</span><span class="p">,</span> <span class="s2">"346c51735f42c37e0712e0b3d2f6476c86ac15863e4445d9e823fe396420d056"</span><span class="p">)</span>
<span class="n">add_versions</span><span class="p">(</span><span class="s2">"github:4.0.2"</span><span class="p">,</span> <span class="s2">"4df1ef0bf73b7148caea1270539ef7bd06607e0ea8aa2fbf1bb34062a097f026"</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="add_versions">add_versions</h4>

<p>用于设置每个源码包的版本和对应的sha256值，具体描述见：<a href="https://xmake.io/#/zh-cn/package/remote_package?id=add_urls">add_urls</a></p>

<h4 id="add_patches">add_patches</h4>

<p>此接口用于针对源码包，在编译安装前，先打对应设置的补丁包，再对其进行编译，并且可支持同时打多个补丁。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"macosx"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">add_patches</span><span class="p">(</span><span class="s2">"1.15"</span><span class="p">,</span> <span class="s2">"https://raw.githubusercontent.com/Homebrew/patches/9be2793af/libiconv/patch-utf8mac.diff"</span><span class="p">,</span>
                        <span class="s2">"e8128732f22f63b5c656659786d2cf76f1450008f36bcf541285268c66cabeab"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>例如，上面的代码，就是针对macosx下编译的时候，打上对应的patch-utf8mac.diff补丁，并且每个补丁后面也是要设置sha256值的，确保完整性。</p>

<h4 id="add_links">add_links</h4>

<p>默认情况下，xmake会去自动检测安装后的库，设置链接关系，但是有时候并不是很准，如果要自己手动调整链接顺序，以及链接名，则可以通过这个接口来设置。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_links</span><span class="p">(</span><span class="s2">"mbedtls"</span><span class="p">,</span> <span class="s2">"mbedx509"</span><span class="p">,</span> <span class="s2">"mbedcrypto"</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="add_syslinks">add_syslinks</h4>

<p>添加一些系统库链接，有些包集成链接的时候，还需要依赖一些系统库，才能链接通过，这个时候可以在包描述里面都附加上去。</p>

<div class="highlighter-rouge"><pre class="highlight"><code>if is_plat("macosx") then
    add_frameworks("CoreGraphics", "CoreFoundation", "Foundation")
elseif is_plat("windows") then
    add_defines("CAIRO_WIN32_STATIC_BUILD=1")
    add_syslinks("gdi32", "msimg32", "user32")
else
    add_syslinks("pthread")
end
</code></pre>
</div>

<h4 id="add_frameworks">add_frameworks</h4>

<p>添加依赖的系统frameworks链接。</p>

<p>示例见：<a href="https://xmake.io/#/zh-cn/package/remote_package?id=add_syslink">add_syslinks</a></p>

<h4 id="add_linkdirs">add_linkdirs</h4>

<p>包的链接库搜索目录也是可以调整的，不过通常都不需要，除非一些库安装完不在prefix/lib下面，而在lib的子目录下，默认搜索不到的话。</p>

<h4 id="add_includedirs">add_includedirs</h4>

<p>添加其他头文件搜索目录。</p>

<h4 id="add_defines">add_defines</h4>

<p>可以对集成的包对外输出一些特定的定义选项。</p>

<h4 id="add_configs">add_configs</h4>

<p>我们可以通过此接口添加每个包的对外输出配置参数：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">package</span><span class="p">(</span><span class="s2">"pcre2"</span><span class="p">)</span>

    <span class="n">set_homepage</span><span class="p">(</span><span class="s2">"https://www.pcre.org/"</span><span class="p">)</span>
    <span class="n">set_description</span><span class="p">(</span><span class="s2">"A Perl Compatible Regular Expressions Library"</span><span class="p">)</span>

    <span class="n">add_configs</span><span class="p">(</span><span class="s2">"bitwidth"</span><span class="p">,</span> <span class="p">{</span><span class="n">description</span> <span class="o">=</span> <span class="s2">"Set the code unit width."</span><span class="p">,</span> <span class="n">default</span> <span class="o">=</span> <span class="s2">"8"</span><span class="p">,</span> <span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"8"</span><span class="p">,</span> <span class="s2">"16"</span><span class="p">,</span> <span class="s2">"32"</span><span class="p">}})</span>

    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
        <span class="kd">local</span> <span class="n">bitwidth</span> <span class="o">=</span> <span class="n">package</span><span class="p">:</span><span class="n">config</span><span class="p">(</span><span class="s2">"bitwidth"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"8"</span>
        <span class="n">package</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"links"</span><span class="p">,</span> <span class="s2">"pcre2-"</span> <span class="o">..</span> <span class="n">bitwidth</span><span class="p">)</span>
        <span class="n">package</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"defines"</span><span class="p">,</span> <span class="s2">"PCRE2_CODE_UNIT_WIDTH="</span> <span class="o">..</span> <span class="n">bitwidth</span><span class="p">)</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>在工程项目里面，我们也可以查看特定包的可配置参数和值列表：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake require --info pcre2
The package info of project:
    require<span class="o">(</span>pcre2<span class="o">)</span>: 
      -&gt; description: A Perl Compatible Regular Expressions Library
      -&gt; version: 10.31
      ...
      -&gt; configs:
         -&gt; bitwidth:
            -&gt; description: Set the code unit width.
            -&gt; values: <span class="o">{</span><span class="s2">"8"</span>,<span class="s2">"16"</span>,<span class="s2">"32"</span><span class="o">}</span>
            -&gt; default: 8
</code></pre>
</div>

<p>然后在项目里面，启用这些配置，编译集成带有特定配置的包：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"pcre2"</span><span class="p">,</span> <span class="p">{</span><span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">bitwidth</span> <span class="o">=</span> <span class="mi">16</span><span class="p">}})</span>
</code></pre>
</div>

<h4 id="on_load">on_load</h4>

<p>这是个可选的接口，如果要更加灵活的动态判断各种平台架构，针对性做设置，可以在这个里面完成，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="kd">local</span> <span class="n">bitwidth</span> <span class="o">=</span> <span class="n">package</span><span class="p">:</span><span class="n">config</span><span class="p">(</span><span class="s2">"bitwidth"</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">"8"</span>
    <span class="n">package</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"links"</span><span class="p">,</span> <span class="s2">"pcre"</span> <span class="o">..</span> <span class="p">(</span><span class="n">bitwidth</span> <span class="o">~=</span> <span class="s2">"8"</span> <span class="ow">and</span> <span class="n">bitwidth</span> <span class="ow">or</span> <span class="s2">""</span><span class="p">))</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">package</span><span class="p">:</span><span class="n">config</span><span class="p">(</span><span class="s2">"shared"</span><span class="p">)</span> <span class="k">then</span>
        <span class="n">package</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"defines"</span><span class="p">,</span> <span class="s2">"PCRE_STATIC"</span><span class="p">)</span>
    <span class="k">end</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>pcre包需要做一些针对bitwidth的判断，才能确定对外输出的链接库名字，还需要针对动态库增加一些defines导出，这个时候在on_load里面设置，就更加灵活了。</p>

<h4 id="on_install">on_install</h4>

<p>这个接口主要用于添加安装脚本，前面的字符串参数用于设置支持的平台，像<code class="highlighter-rouge">on_load</code>, <code class="highlighter-rouge">on_test</code>等其他脚本域也是同样支持的。</p>

<h5 id="平台过滤">平台过滤</h5>

<p>完整的过滤语法如下：<code class="highlighter-rouge">plat|arch1,arch2@host|arch1,arch2</code></p>

<p>看上去非常的复杂，其实很简单，其中每个阶段都是可选的，可部分省略，对应：<code class="highlighter-rouge">编译平台|编译架构@主机平台|主机架构</code></p>

<p>如果不设置任何平台过滤条件，那么默认全平台支持，里面的脚本对所有平台生效，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="c1">-- TODO</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果安装脚本对特定平台生效，那么直接指定对应的编译平台，可以同时指定多个：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="s2">"linux"</span><span class="p">,</span> <span class="s2">"macosx"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="c1">-- TODO</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果还要细分到指定架构才能生效，可以这么写：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="s2">"linux|x86_64"</span><span class="p">,</span> <span class="s2">"iphoneos|arm64"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="c1">-- TODO</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果还要限制执行的主机环境平台和架构，可以在后面追加<code class="highlighter-rouge">@host|arch</code>，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="s2">"mingw@windows"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="c1">-- TODO</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>意思就是仅对windows下编译mingw平台生效。</p>

<p>我们也可以不指定比那一平台和架构，仅设置主机平台和架构，这通常用于描述一些跟编译工具相关的依赖包，只能在主机环境运行。</p>

<p>例如，我们编译的包，依赖了cmake，需要添加cmake的包描述，那么里面编译安装环境，只能是主机平台：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="s2">"@windows"</span><span class="p">,</span> <span class="s2">"@linux"</span><span class="p">,</span> <span class="s2">"@macosx"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="c1">-- TODO</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>其他一些例子：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="c1">-- `@linux`</span>
<span class="c1">-- `@linux|x86_64`</span>
<span class="c1">-- `@macosx,linux`</span>
<span class="c1">-- `android@macosx,linux`</span>
<span class="c1">-- `android|armv7-a@macosx,linux`</span>
<span class="c1">-- `android|armv7-a@macosx,linux|x86_64`</span>
<span class="c1">-- `android|armv7-a@linux|x86_64`</span>
</code></pre>
</div>

<h5 id="编译工具">编译工具</h5>

<p>我们内置了一些安装常用编译工具脚本，用于针对不同源码依赖的构建工具链，进行方便的构架支持，例如：autoconf, cmake, meson等，</p>

<h6 id="xmake">xmake</h6>

<p>如果是基于xmake的依赖包，那么集成起来就非常简单了，xmake对其做了非常好的内置集成支持，可以直接对其进行跨平台编译支持，一般情况下只需要：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.xmake"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">)</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果要传递一些特有的编译配置参数：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.xmake"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="p">{</span><span class="s2">"--xxx=y"</span><span class="p">})</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h6 id="cmake">cmake</h6>

<p>如果是基于cmake的包，集成起来也很简答，通常也只需要设置一些配置参数即可，不过还需要先添加上cmake的依赖才行：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_deps</span><span class="p">(</span><span class="s2">"cmake"</span><span class="p">)</span>
<span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.cmake"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="p">{</span><span class="s2">"-Dxxx=ON"</span><span class="p">})</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h6 id="autoconf">autoconf</h6>

<p>如果是基于autoconf的包，集成方式跟cmake类似，只是传递的配置参数不同而已，不过通常情况下，unix系统都内置了autoconf系列工具，所以不加相关依赖也没事。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.autoconf"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="p">{</span><span class="s2">"--enable-shared=no"</span><span class="p">})</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>不过，有些源码包用系统内置的autoconf可能不能完全满足，那么可以加上autoconf系列依赖，对其进行构建：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_deps</span><span class="p">(</span><span class="s2">"autoconf"</span><span class="p">,</span> <span class="s2">"automake"</span><span class="p">,</span> <span class="s2">"libtool"</span><span class="p">,</span> <span class="s2">"pkg-config"</span><span class="p">)</span>
<span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.autoconf"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="p">{</span><span class="s2">"--enable-shared=no"</span><span class="p">})</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h6 id="meson">meson</h6>

<p>如果是meson，还需要加上ninja的依赖来执行构建才行。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_deps</span><span class="p">(</span><span class="s2">"meson"</span><span class="p">,</span> <span class="s2">"ninja"</span><span class="p">)</span>
<span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.meson"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="p">{</span><span class="s2">"-Dxxx=ON"</span><span class="p">})</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="on_test">on_test</h4>

<p>安装后，需要设置对应的测试脚本，执行一些测试，确保安装包的可靠性，如果测试不通过，则会撤销整个安装包。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_test</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="nb">assert</span><span class="p">(</span><span class="n">package</span><span class="p">:</span><span class="n">has_cfuncs</span><span class="p">(</span><span class="s2">"inflate"</span><span class="p">,</span> <span class="p">{</span><span class="n">includes</span> <span class="o">=</span> <span class="s2">"zlib.h"</span><span class="p">}))</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>上面的脚本调用包内置的<code class="highlighter-rouge">has_cfuncs</code>接口，检测安装后的包是否存在zlib.h头文件，以及库和头文件里面是否存在<code class="highlighter-rouge">inflate</code>这个接口函数。</p>

<p>xmake会去尝试编译链接来做测试，<code class="highlighter-rouge">has_cfuncs</code>用于检测c函数，而<code class="highlighter-rouge">has_cxxfuncs</code>则可以检测c++库函数。</p>

<p>而includes里面可以设置多个头文件，例如：<code class="highlighter-rouge">includes = {"xxx.h", "yyy.h"}</code></p>

<p>我们还可以传递一些自己的编译参数进去检测，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_test</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="nb">assert</span><span class="p">(</span><span class="n">package</span><span class="p">:</span><span class="n">has_cxxfuncs</span><span class="p">(</span><span class="s2">"func1"</span><span class="p">,</span> <span class="p">{</span><span class="n">includes</span> <span class="o">=</span> <span class="s2">"xxx.h"</span><span class="p">,</span> <span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">defines</span> <span class="o">=</span> <span class="s2">"c++14"</span><span class="p">,</span> <span class="n">cxflags</span> <span class="o">=</span> <span class="s2">"-Dxxx"</span><span class="p">}}))</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>我们也可以通过<code class="highlighter-rouge">check_csnippets</code>和<code class="highlighter-rouge">check_cxxsnippets</code>检测一个代码片段：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_test</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="nb">assert</span><span class="p">(</span><span class="n">package</span><span class="p">:</span><span class="n">check_cxxsnippets</span><span class="p">({</span><span class="n">test</span> <span class="o">=</span> <span class="s">[[
        #include &lt;boost/algorithm/string.hpp&gt;
        #include &lt;string&gt;
        #include &lt;vector&gt;
        #include &lt;assert.h&gt;
        using namespace boost::algorithm;
        using namespace std;
        static void test() {
            string str("a,b");
            vector&lt;string&gt; strVec;
            split(strVec, str, is_any_of(","));
            assert(strVec.size()==2);
            assert(strVec[0]=="a");
            assert(strVec[1]=="b");
        }
    ]]</span><span class="p">},</span> <span class="p">{</span><span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">languages</span> <span class="o">=</span> <span class="s2">"c++14"</span><span class="p">}}))</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果是可执行包，也可以通过尝试运行来检测：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_test</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">os</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"xxx --help"</span><span class="p">)</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果运行失败，那么测试不会通过。</p>

<h3 id="扩展配置参数">扩展配置参数</h3>

<p>详情见：<a href="https://xmake.io/#/zh-cn/package/remote_package?id=add_configs">add_configs</a></p>

<h3 id="内置配置参数">内置配置参数</h3>

<p>除了可以通过<a href="https://xmake.io/#/zh-cn/package/remote_package?id=add_configs">add_configs</a>设置一些扩展的配置参数以外，xmake还提供了一些内置的配置参数，可以使用</p>

<h4 id="启用调试包">启用调试包</h4>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"xxx"</span><span class="p">,</span> <span class="p">{</span><span class="n">debug</span> <span class="o">=</span> <span class="kc">true</span><span class="p">})</span>
</code></pre>
</div>

<p>包描述里面必须有相关处理才能支持：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="kd">local</span> <span class="n">configs</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">if</span> <span class="n">package</span><span class="p">:</span><span class="n">debug</span><span class="p">()</span> <span class="k">then</span>
        <span class="nb">table.insert</span><span class="p">(</span><span class="n">configs</span><span class="p">,</span> <span class="s2">"--enable-debug"</span><span class="p">)</span>
    <span class="k">end</span>
    <span class="n">import</span><span class="p">(</span><span class="s2">"package.tools.autoconf"</span><span class="p">).</span><span class="n">install</span><span class="p">(</span><span class="n">package</span><span class="p">)</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="设置msvc运行时库">设置msvc运行时库</h4>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"xxx"</span><span class="p">,</span> <span class="p">{</span><span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">vs_runtime</span> <span class="o">=</span> <span class="s2">"MT"</span><span class="p">}})</span>
</code></pre>
</div>

<p>通常情况下，通过<code class="highlighter-rouge">import("package.tools.autoconf").install</code>等内置工具脚本安装的包，内部都对vs_runtime自动处理过了。</p>

<p>但是如果是一些特殊的源码包，构建规则比较特殊，那么需要自己处理了：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">on_install</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">io</span><span class="p">.</span><span class="n">gsub</span><span class="p">(</span><span class="s2">"build/Makefile.win32.common"</span><span class="p">,</span> <span class="s2">"%-MD"</span><span class="p">,</span> <span class="s2">"-"</span> <span class="o">..</span> <span class="n">package</span><span class="p">:</span><span class="n">config</span><span class="p">(</span><span class="s2">"vs_runtime"</span><span class="p">))</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h3 id="添加环境变量">添加环境变量</h3>

<p>对于一些库，里面也带了可执行的工具，如果需要在集成包的时候，使用上这些工具，那么也可以设置上对应PATH环境变量：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">package</span><span class="p">(</span><span class="s2">"luajit"</span><span class="p">)</span>
    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"windows"</span><span class="p">)</span> <span class="k">then</span>
            <span class="n">package</span><span class="p">:</span><span class="n">addenv</span><span class="p">(</span><span class="s2">"PATH"</span><span class="p">,</span> <span class="s2">"lib"</span><span class="p">)</span>
        <span class="k">end</span>
        <span class="n">package</span><span class="p">:</span><span class="n">addenv</span><span class="p">(</span><span class="s2">"PATH"</span><span class="p">,</span> <span class="s2">"bin"</span><span class="p">)</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>而在项目工程中，只有通过<code class="highlighter-rouge">add_packages</code>集成对应的包后，对应的环境变量才会生效。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"luajit"</span><span class="p">)</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_packages</span><span class="p">(</span><span class="s2">"luajit"</span><span class="p">)</span>
    <span class="n">after_run</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
        <span class="n">os</span><span class="p">.</span><span class="n">exec</span><span class="p">(</span><span class="s2">"luajit --version"</span><span class="p">)</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h3 id="安装二进制包">安装二进制包</h3>

<p>xmake也是支持直接引用二进制版本包，直接安装使用，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"windows"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">set_urls</span><span class="p">(</span><span class="s2">"https://www.libsdl.org/release/SDL2-devel-$(version)-VC.zip"</span><span class="p">)</span>
    <span class="n">add_versions</span><span class="p">(</span><span class="s2">"2.0.8"</span><span class="p">,</span> <span class="s2">"68505e1f7c16d8538e116405411205355a029dcf2df738dbbc768b2fe95d20fd"</span><span class="p">)</span>
<span class="k">end</span>

<span class="n">on_install</span><span class="p">(</span><span class="s2">"windows"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">package</span><span class="p">)</span>
    <span class="n">os</span><span class="p">.</span><span class="n">cp</span><span class="p">(</span><span class="s2">"include"</span><span class="p">,</span> <span class="n">package</span><span class="p">:</span><span class="n">installdir</span><span class="p">())</span>
    <span class="n">os</span><span class="p">.</span><span class="n">cp</span><span class="p">(</span><span class="s2">"lib/$(arch)/*.lib"</span><span class="p">,</span> <span class="n">package</span><span class="p">:</span><span class="n">installdir</span><span class="p">(</span><span class="s2">"lib"</span><span class="p">))</span>
    <span class="n">os</span><span class="p">.</span><span class="n">cp</span><span class="p">(</span><span class="s2">"lib/$(arch)/*.dll"</span><span class="p">,</span> <span class="n">package</span><span class="p">:</span><span class="n">installdir</span><span class="p">(</span><span class="s2">"lib"</span><span class="p">))</span>
<span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h3 id="本地测试">本地测试</h3>

<p>如果在本地xmake-repo仓库中，已经添加和制作好了新的包，可以在本地运行测试下，是否通过，如果测试通过，即可提交pr到官方仓库，请求merge。</p>

<p>我们可以执行下面的脚本进行测试指定包：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="nb">cd </span>xmake-repo
xmake l scripts/test.lua -v -D zlib
</code></pre>
</div>

<p>上面的命令，会强制重新下载和安装zlib包，测试整个安装流程是否ok，加上<code class="highlighter-rouge">-v -D</code>是为了可以看到完整详细的日志信息和出错信息，方便调试分析。</p>

<p>如果网络环境不好，不想每次测试都去重新下载所有依赖，可以加上<code class="highlighter-rouge">--shallow</code>参数来执行，这个参数告诉脚本，仅仅重新解压本地缓存的zlib源码包，重新执行安装命令，但不会下载各种依赖。</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="nb">cd </span>xmake-repo
xmake l scripts/test.lua -v -D --shallow zlib
</code></pre>
</div>

<p>如果我们想测试其他平台的包规则是否正常，比如: android, iphoneos等平台，可以通过<code class="highlighter-rouge">-p/--plat</code>或者<code class="highlighter-rouge">-a/--arch</code>来指定。</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="nb">cd </span>xmake-repo
xmake l scripts/test.lua -v -D --shallow -p iphoneos -a arm64 zlib
xmake l scripts/test.lua -v -D --shallow -p android --ndk<span class="o">=</span>/xxxx zlib
</code></pre>
</div>

<h2 id="提交包到官方仓库">提交包到官方仓库</h2>

<p>目前这个特性刚完成不久，目前官方仓库的包还不是很多，有些包也许还不支持部分平台，不过这并不是太大问题，后期迭代几个版本后，我会不断扩充完善包仓库。</p>

<p>如果你需要的包，当前的官方仓库还没有收录，可以提交issues或者自己可以在本地调通后，贡献提交到官方仓库：<a href="https://github.com/xmake-io/xmake-repo">xmake-repo</a></p>

<p>详细的贡献说明，见：<a href="https://github.com/xmake-io/xmake-repo/blob/master/CONTRIBUTING.md">CONTRIBUTING.md</a></p>

<p>关于如何制作自己的包，可以看下上文：<a href="https://xmake.io/#/zh-cn/package/remote_package?id=%e6%b7%bb%e5%8a%a0%e5%8c%85%e5%88%b0%e4%bb%93%e5%ba%93">添加包到仓库</a>。</p>


        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
                    
                    <h2 id="english">English</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/2019/08/09/xmake-upload-package/">Create and upload C/C++ packages to xmake's official repository
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        
            </ul>
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2019/06/17/xmake-update-v2.2.7/">xmake v2.2.7 发布, 改进Cuda项目构建</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2019/08/22/xmake-update-v2.2.8/">xmake v2.2.8 发布, 新版vs工程生成插件</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
